package project3cs424.src;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class AirportScraper {

	public static void main(String[] args) throws IOException {
		String inputFileName = "GlobalAirportDatabase/GlobalAirportDatabase.txt";
		BufferedReader inputFileReader = new BufferedReader(new FileReader(
				new File(inputFileName)));
		BufferedWriter outputFileWriter = new BufferedWriter(new FileWriter(
				new File("GlobalAirportDatabase/airportstrial")));
		while (inputFileReader.ready()) {
			String inputFileLine = inputFileReader.readLine();
			String[] inputFileLineParts = inputFileLine.split(":");
			String countryName = inputFileLineParts[4];
			if (countryName.equals("USA")) {
				//outputFileWriter.write(inputFileLine + "\n");
				String fourCode=inputFileLineParts[0];
				String threeCode=inputFileLineParts[1];
				String airportName=inputFileLineParts[2];
				String place=inputFileLineParts[3];
				String country=inputFileLineParts[4];
				float latDeg=Float.parseFloat(inputFileLineParts[5]);
				float latMin=Float.parseFloat(inputFileLineParts[6]);
				float latSec=Float.parseFloat(inputFileLineParts[7]);
				String latDir=inputFileLineParts[8];
				float lngDeg=Float.parseFloat(inputFileLineParts[9]);
				float lngMin=Float.parseFloat(inputFileLineParts[10]);
				float lngSec=Float.parseFloat(inputFileLineParts[11]);
				String lngDir=inputFileLineParts[12];
				float latitude;
				float longitude;
				
				if(latDir.equals("N"))
				{
					latitude=((latDeg)+(latMin/60)+(latSec/3600))*(-1);
				}
				else
				{
					latitude=((latDeg)+(latMin/60)+(latSec/3600));
				}
				
				if(lngDir.equals("E"))
				{
					longitude=(lngDeg)+(lngMin/60)+(lngSec/3600);
				}
				else
				{
					longitude=((lngDeg)+(lngMin/60)+(lngSec/3600))*(-1);
				}
				outputFileWriter.write(fourCode+"\t"+threeCode+"\t"+airportName+"\t"+place+"\t"+country+"\t"+latitude+"\t"+longitude+"\n");
				
			}

		}
		outputFileWriter.close();
	}

}
